<template>
    <div class="e-form">
        <BillTop @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <div style="width: 100%" class="form">
                <el-form
                    :model="formData"
                    label-width="150px"
                    :rules="formDataRules"
                    ref="formDataRef"
                >
                    <el-row>
                        <!-- <el-col :span="12">
                            <el-form-item label="处置编号">
                                {{ formData.billNo }}
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="12">
                            <el-form-item label="处置日期">
                                <el-date-picker
                                    v-model="formData.billDate"
                                    type="date"
                                    placeholder="选择日期"
                                    value-format="yyyy-MM-dd"
                                    :clearable="false"
                                >
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="结算单位" prop="customer">
                                <el-input v-model="formData.customer" readonly>
                                    <i
                                        slot="suffix"
                                        class="
                                            el-input__icon
                                            el-icon-document-copy
                                        "
                                        @click="customerClick"
                                    ></i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="经办人" prop="founderName">
                                <el-input
                                    v-model="formData.founderName"
                                    readonly
                                >
                                    <i
                                        slot="suffix"
                                        class="
                                            el-input__icon
                                            el-icon-document-copy
                                        "
                                        @click="founderNameClick"
                                    ></i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- <el-row>
                        <el-col :span="12">
                            <el-form-item label="发票编号" prop="invoice">
                                <el-input
                                    placeholder="请填写发票号"
                                    v-model="formData.invoice"
                                    v-enterInteger1
                                    v-if="!isRedSingle"
                                ></el-input>
                                <span v-else>{{ formData.invoice }}</span>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="机构" prop="orgName">
                                {{ formData.orgName }}
                            </el-form-item>
                        </el-col>
                    </el-row> -->
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="税率%" prop="taxRate">
                                {{ formData.taxRate }}
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="12">
                            <el-form-item label="税额(元)" prop="taxAmount">
                                <el-input
                                    placeholder="请填写"
                                    v-model="formData.taxAmount"
                                    v-enterFloat
                                ></el-input>
                            </el-form-item>
                        </el-col> -->
                    </el-row>
                    <el-row>
                        <!-- <el-col :span="12">
                            <el-form-item
                                label="处置金额(元)"
                                prop="disposalAmount"
                            >
                                {{ formData.disposalAmount }}
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="12">
                            <el-form-item label="机构" prop="orgName">
                                {{ formData.orgName }}
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <!-- <el-row>
                        <el-col :span="12">
                            <el-form-item
                                label="冲预付款"
                                prop="rushAdvanceCharge"
                            >
                                <el-checkbox
                                    v-model="formData.rushAdvanceCharge"
                                    label=""
                                    style="margin-right: 10px"
                                    :disabled="isRedSingle"
                                ></el-checkbox>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="冲预付款金额(元)"
                                :prop="
                                    formData.rushAdvanceCharge
                                        ? 'rushAdvanceChargePrice'
                                        : 'rushAdvanceChargePrices'
                                "
                            >
                                <el-input
                                    placeholder="请填写"
                                    v-model="formData.rushAdvanceChargePrice"
                                    v-enterInteger1
                                    v-if="!isRedSingle"
                                ></el-input>
                                <span v-else>{{
                                    formData.rushAdvanceChargePrice
                                }}</span>
                            </el-form-item>
                        </el-col>
                    </el-row> -->

                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="币种" prop="currencyId">
                                <el-select
                                    v-model="formData.currencyId"
                                    placeholder="请选择"
                                    @change="currencyChange"
                                >
                                    <el-option
                                        v-for="item in currencyForm"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="12">
                            <el-form-item label="传输财务共享" prop="share">
                                {{ formData.share }}
                            </el-form-item>
                        </el-col> -->
                    </el-row>

                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="本位币" prop="baseCurName">
                                {{ formData.baseCurName }}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="人民币汇率">
                                {{ formData.rmbRate }}
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="本位币汇率" prop="baseCurRate">
                                <el-input
                                    placeholder="请填写"
                                    v-model="formData.baseCurRate"
                                    v-enter-float2="{
                                        set: formData,
                                        key: 'baseCurRate'
                                    }"
                                    v-if="!isBaseCurRate"
                                    @blur="baseCurRateBlur"
                                ></el-input>
                                <span v-else>{{ formData.baseCurRate }}</span>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="状态" prop="state">
                                {{  showState(formData.state) }}
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="12">
                            <el-form-item label="税价合计(本位币)">
                                {{ formData.taxScSumCost }}
                            </el-form-item>
                        </el-col> -->
                    </el-row>
                    <!-- <el-row>
                        <el-col :span="12">
                            <el-form-item label="冲预付款金额(本位币)">
                                {{ formData.rushScSumCost }}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="状态">
                                {{  showState(formData.state) }}
                            </el-form-item>
                        </el-col>
                    </el-row> -->

                    <el-row>
                        <el-form-item label="备注">
                            <el-input
                                type="textarea"
                                :rows="2"
                                resize="none"
                                placeholder="请输入内容"
                                v-model="formData.remarks"
                            >
                            </el-input>
                        </el-form-item>
                    </el-row>
                </el-form>
            </div>
        </div>
        <div class="buttons">
            <el-button type="primary" style="background: #2e61d7" @click="save"
                >保存</el-button
            >

            <el-button @click="handleClose">取消</el-button>
        </div>
    </div>
</template>

<script>
import '@/utils/jquery.scrollTo.min'
import { mapState, mapMutations } from 'vuex'
import { formatDate } from '@/utils/common'
import request from '@/api/settlementManagement/equipmentBilling/equipmentDisposal'
import publics from '@/api/settlementManagement/equipmentBilling/public'

export default {
    data () {
        return {
            //表单数据
            formData: {
                baseCurDisposalAmount: 0,
                baseCurId: '',
                baseCurName: '',
                baseCurRate: null,
                billDate: formatDate(new Date(), 'yyyy-MM-dd'),
                billId: '',
                billNo: '',
                currency: '',
                currencyId: '',
                customer: '',
                customerId: '',
                disposalAmount: 0,
                evaluateGrade: '',
                founderId: '',
                founderName: '',
                nullifyCreated: '',
                nullifyCreator: '',
                nullifyCreatorId: '',
                nullifyDesciption: '',
                nullifyReason: '',
                orgId: '',
                orgName: '',
                redState: 0,
                remarks: '',
                rmbId: '',
                rmbName: '',
                rmbRate: 0,
                rmbSumDisposalAmount: 0,
                share: '',
                state: 0,
                taxRate: '',
                taxAmount: 0,
                workId: '',
                contractId: '',
                contractName: '',
                contractNo: ''
            },
            orgId: '', //机构id
            orgName: '', //机构名称
            isSkip: false, //是否跳转

            formDataRules: {
                customer: [
                    {
                        required: true,
                        message: '请选择结算单位',
                        trigger: 'blur'
                    }
                ],
                taxRate: [
                    { required: true, message: '请输入税率', trigger: 'change' }
                ],
                taxAmount: [
                    {
                        required: true,
                        message: '请输入税额(元)',
                        trigger: 'blur'
                    }
                ],
                disposalAmount: [
                    {
                        required: true,
                        message: '请输入处置金额',
                        trigger: 'blur'
                    }
                ],
                currencyId: [
                    { required: true, message: '请选择币种', trigger: 'blur' }
                ],
                baseCurName: [
                    {
                        required: true,
                        message: '请选择本位币',
                        trigger: 'blur'
                    }
                ],
                baseCurRate: [
                    {
                        required: true,
                        message: '请填写本位币汇率',
                        trigger: 'blur'
                    }
                ]
            },
            redFormData: null,
            isBaseCurRate: true
        }
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            currencyForm: state => state.equip.equipData.currencyForm //币种
        }),

    },
    watch: {
        // // 税额 = 不含税金额*税率
        // 'formData.disposalAmount' () {
        //     this.formData.taxAmount = (
        //         +this.formData.disposalAmount *
        //         (+this.formData.taxRate / 100)
        //     ).toFixed(2)
        // },
        // // 税额 = 不含税金额*税率
        // 'formData.taxRate' () {
        //     this.formData.taxAmount = (
        //         +this.formData.disposalAmount *
        //         (+this.formData.taxRate / 100)
        //     ).toFixed(2)
        // },

        // // 冲预付款金额(本位币) = 冲预付款金额 * 本位币汇率
        // 'formData.rushAdvanceChargePrice' () {
        //     this.formData.rushScSumCost =
        //         +this.formData.rushAdvanceChargePrice *
        //         +this.formData.baseCurRate
        // },
        // // 税价合计(本位币) = 税价合计 * 本位币汇率
        // 'formData.totalTaxPrice' () {
        //     this.formData.taxScSumCost =
        //         +this.formData.totalTaxPrice * +this.formData.baseCurRate
        // },
        // 本位币和币种一样的时候，值为1；不一样的时候，填写正数，不能为0，保留4位小数
        'formData.currency' () {
            if (this.formData.currency == this.formData.baseCurName) {
                this.isBaseCurRate = true
                this.formData.baseCurRate = 1
            } else {
                this.isBaseCurRate = false
                this.formData.baseCurRate = null
            }
        }

        // // 冲预付款勾选中,冲预付款金额必填
        // 'formData.rushAdvanceCharge' () {
        //     if (!this.formData.rushAdvanceCharge) {
        //         this.$refs.formDataRef.clearValidate('rushAdvanceChargePrice')
        //         this.formData.rushAdvanceChargePrice = ''
        //     }
        // }
    },
    components: {},
    created () {
        let data = JSON.parse(localStorage.getItem('vuex'))
        this.orgId = data.userInfo.orgInfo.orgId
        this.orgName = data.userInfo.orgInfo.orgName
        this.formData.orgName = this.orgName
        this.formData.orgId = this.orgId
        const { baseCyByOrgId } = this.userInfo
        this.formData.baseCurName = baseCyByOrgId.currencyName //本位币
        this.formData.baseCurId = baseCyByOrgId.currency
    },
    methods: {
        ...mapMutations(['setSelectedInfo']),
        // 基本信息-客户:选择设备废旧处置合同
        async customerClick () {
            // this.setSelectedInfo(this.formData.contractId)
            // openAppClient('/showSelectedData', '选择结果')
            let res = await request.customerClick(this.formData.contractId ? [this.formData.contractId] : [])
            if (res.length < 1) {
                this.formData.contractId = ''
                this.formData.contractName = ''
                this.formData.contractNo = ''
                this.formData.customer = ''
                this.formData.taxRate = ''
                return
            }
            let data = res[0]
            if (this.formData.contractId !== data.contractId) {
                this.formData.contractId = data.contractId // 合同id
                this.formData.contractName = data.contractName // 合同名称
                this.formData.contractNo = data.contractNumber // 合同编号
                this.formData.customer = data.firstPartyName // 结算单位
                this.formData.taxRate = data.taxRate // 税率
            }
        },
        // 基本信息-选择经办人
        async founderNameClick () {
            let res = await request.getBorrow()
            this.formData.founderName = res[0].userName
            this.formData.founderId = res[0].userId
        },

        // 当币种切换时,获取人民币汇率
        async currencyChange (val) {
            this.formData.currency = this.currencyForm.find(
                x => x.value === val
            ).label //币种名称
            try {
                let res =  await publics.getRmbRate({
                //获取机构下某币种的人民币汇率
                    currency: val,
                    orgId: this.userInfo.orgInfo.orgId
                })
                this.formData.rmbRate = res.rate
            }catch(err) {
                this.formData.rmbRate = ''
            }
        },
        // 本位币汇率不能为0
        baseCurRateBlur () {
            if (this.formData.baseCurRate == 0) {
                this.formData.baseCurRate = null
            }
        },

        //保存按钮
        save () {
            this.$refs.formDataRef.validate(valid => {
                if (!valid) return false
                request.basicInfoAdd(this.formData).then(res => {
                    this.clientPop(
                        'info',
                        '保存成功,是否继续',
                        () => {
                            this.$router.replace({
                                path: '/equipmentDisposalSetEdit',
                                query: {
                                    billid: res,
                                    name: 'disposalDtl'
                                }
                            })
                        },
                        null,
                        this.handleClose
                    )
                })
            })
        },
        //取消按钮
        handleClose () {
            this.$router.replace('/equipmentDisposalSetList')
        },
        showState (state) {
            const _state = this.$store.state.equip.equipData.settleState
            const obj = _state.find(x => x.value === Number(state))
            return obj.label
        }
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    margin-bottom: 80px;
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
